﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-03-18
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;

namespace IFramework
{
	public class EditorUtil
	{
        [MenuItem("IFramework/Copy Asset Path")]
        public static void CopyAssetPath()
        {
            if (EditorApplication.isCompiling)
            {
                return;
            }
            string path = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
            GUIUtility.systemCopyBuffer = path;
        }
        [MenuItem("IFramework/Editor/Close")]
        public static void Close()
        {
            Environment.Exit(0);
            EditorApplication.Exit(0);
        }
        [MenuItem("IFramework/Editor/Restart")]
        public static void Restart()
        {
            string unityPath = Process.GetCurrentProcess().MainModule.FileName;
            string assetPath = Application.dataPath.GetDirPath();
            string batPath = FrameWorkInfo.GetFrameWorkEditorPath().CombinePath(@"ReStart.bat").ToAbsPath();
            int processId = Process.GetCurrentProcess().Id;
            CreatReStartBat(batPath);
            CreateProcess(batPath, string.Format("\"{0}\" \"{1}\" ", unityPath, assetPath, unityPath.Length));
            Process.GetCurrentProcess().Kill();
        }
        //创建cmd
        public static Process CreateProcess(string cmd, string args, string workingDir = "")
        {
            var pStartInfo = new ProcessStartInfo(cmd);
            pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            pStartInfo.Arguments = args;
            pStartInfo.CreateNoWindow = false;
            pStartInfo.UseShellExecute = true;
            pStartInfo.RedirectStandardError = false;
            pStartInfo.RedirectStandardInput = false;
            pStartInfo.RedirectStandardOutput = false;
            if (!string.IsNullOrEmpty(workingDir))
            {
                pStartInfo.WorkingDirectory = workingDir;
            }
            return Process.Start(pStartInfo);
        }
        private static  void CreatReStartBat(string batPath)
        {
            if (File.Exists(batPath)) return;
            using (FileStream fs = new FileStream(batPath, FileMode.OpenOrCreate))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    fs.Lock(0, fs.Length);
                    sw.WriteLine("@echo off");

                    sw.WriteLine("set unityPath=%~1%");
                    sw.WriteLine("set projectPath=%~2%");
                    sw.WriteLine("for /f \"delims = \"\" tokens = 1\" %%v in ( %unityPath% ) do ( set unityPath=%%v )");
                    sw.WriteLine("for /f \"delims = \"\" tokens = 1\" %%v in ( %projectPath% ) do ( set projectPath=%%v )");
                    sw.WriteLine("start %unityPath% -projectPath %projectPath%");
                    //sw.WriteLine("pause");
                    fs.Unlock(0, fs.Length);
                    sw.Flush();
                    fs.Flush();

                    sw.Close();
                    fs.Close();
                }
            }
            AssetDatabase.Refresh();
        }
#if !UNITY_2018
        [InitializeOnLoadMethod]
        public static void InitializeOnLoadMethod()
        {
            EditorApplication.wantsToQuit -= OnQuit;
            EditorApplication.wantsToQuit += OnQuit;
        }
        private static bool OnQuit()
        {
            EditorUtility.DisplayDialog("我就是不让你关闭unity", "我就是不让你关闭unity", "呵呵");
            return false; //return true表示可以关闭unity编辑器
        }
#endif
        public static EditorBuildSettingsScene[] ScenesInBuildSetting()
        {
            return EditorBuildSettings.scenes;
        }
        public static string[] GetScenesInBuildSetting()
        {
            List<string> levels = new List<string>();
            for (int i = 0; i < EditorBuildSettings.scenes.Length; ++i)
            {
                if (EditorBuildSettings.scenes[i].enabled)
                    levels.Add(EditorBuildSettings.scenes[i].path);
            }

            return levels.ToArray();
        }

        public static string GetBuildTargetName(BuildTarget target)
        {
            string name = PlayerSettings.productName + "_" + PlayerSettings.bundleVersion;
            if (target == BuildTarget.Android)
            {
                return  name + PlayerSettings.Android.bundleVersionCode + ".apk";
            }
            if (target == BuildTarget.StandaloneWindows || target == BuildTarget.StandaloneWindows64)
            {
                return  name + PlayerSettings.Android.bundleVersionCode + ".exe";
            }
            if
#if UNITY_2017_3_OR_NEWER
            (target == BuildTarget.StandaloneOSX)
#else
            (target == BuildTarget.StandaloneOSXIntel || target == BuildTarget.StandaloneOSXIntel64 || target == BuildTarget.StandaloneOSX)
#endif
            {
                return  name + ".app";
            }
            if (target == BuildTarget.iOS)
            {
                return "iOS";
            }
            return null;
            //if (target == BuildTarget.WebGL)
            //{
            //    return "/web";
            //}

        }
      

    }


}
